import java.awt.Button;

import ucigame.*;
// need to implement TermKalahGame still
public class ui3 extends Ucigame
{
    Sprite button1, button2, button3, button4;
    Sprite player1, player2;
    Sprite scoreBoard1,scoreBoard2;
    int p1 = 0, p2 = 1;
    Sprite hole[][] = new Sprite[2][6];
    int move;
    int gamegrid[][] = new int[2][6];
    int score1, score2;
    String[] players = new String[2];	
    TermKalahGame game;
    
    

    public void setup()
    {
        window.size(1200, 600);
        window.title("Mancala");
        framerate(30);
        window.showFPS();
        
        for(int i=0; i<=5; i++)
        	for(int j=0; j<=1;j++)
        		gamegrid[j][i]=randomInt(0,20);
        score1=randomInt(0,20);
        score2=randomInt(0,20);
        
        players[0] =  "interactive";
        players[1] = "group1";
        canvas.background(0);
        
        
       
       
        startScene("Title");
    }
    public void startTitle()
    {
        
    	button1 = makeButton("Play",
                 getImage("Media/PlayButton.png", 0, 255, 0),
                 158, 30);
        button1.position(330,500);
        button2 = makeButton("Options",
                getImage("Media/OptionsButton.png", 0, 255, 0),
                158, 30);
        button2.position(530,500);
        button3 = makeButton("Inst",
                getImage("Media/InstButton.png", 0, 255, 0),
                158, 30);
        button3.position(730,500);
       
    }   
    public void drawTitle()
    {
        canvas.clear();
        canvas.background(getImage("Media/title.png"));
        button1.draw();      
        button2.draw();    
        button3.draw();    
        
    }
    public void onClickPlay()
    {
    	startScene("Game");
    }
    public void onClickOptions()
    {
    	startScene("OptionsScreen");
    }
    public void onClickInst()
    {
    	startScene("InstrucScreen");
    }
    public void startGame()
    {
    	canvas.font("Times New Roman", BOLD, 27);
    	MancalaGameState gs = new KalahPieGameState(4);
 		game = new TermKalahGame(gs, players[p1], players[p2]);
 		scoreBoard1 = makeSprite(193,424);
 		scoreBoard2 = makeSprite(193,424);
 		
 		scoreBoard1.addFrames(getImage("Media/score.png"), 
 				0,0,
 				0,423,
 				0,846,
 				0,1268,
 				0,1694,
 				0,2116,
 				0,2539,
 				0,2964,
 				0,3388,
 				0,3811,
 				0,4236,
 				0,4660,
 				0,5083,
 				0,5508,
 				0,5931,
 				0,6355
 				);
 		
 		scoreBoard2.addFrames(getImage("Media/score.png"), 
 				0,0,
 				0,423,
 				0,846,
 				0,1268,
 				0,1694,
 				0,2116,
 				0,2539,
 				0,2964,
 				0,3388,
 				0,3811,
 				0,4236,
 				0,4660,
 				0,5083,
 				0,5508,
 				0,5931,
 				0,6355
 				);
    	for(int i=0; i<=5; i++)
        	for(int j=0; j<=1;j++)
    		{
    			hole[j][i] = makeSprite(100, 100);
    			hole[j][i].addFrames(getImage("Media/holes.png"), 
    			       0,0,
    			       0,100,
    			       0,200,
    			       0,300,
    			       0,400,
    			       0,500,
    			       0,600,
    			       0,700,
    			       0,800,
    			       0,900,
    			       0,1000,
    			       0,1100,
    			       0,1200,
    			       0,1300,
    			       0,1400,
    			       0,1500);
    		}
        	
    	
    	hole[0][0].position(255,140);    	
    	hole[0][1].position(375,140);    	
    	hole[0][2].position(495,140);    
    	hole[0][3].position(615,140);    
    	hole[0][4].position(735,140);    
    	hole[0][5].position(855,140);    
    	
    	hole[1][0].position(255,340);
    	hole[1][1].position(375,340);    
    	hole[1][2].position(495,340);
    	hole[1][3].position(615,340);    	
    	hole[1][4].position(735,340);    
    	hole[1][5].position(855,340);  	
    	
    	scoreBoard1.position(35, 80);
    	scoreBoard2.position(980, 80);
    
    	
    }
    public void drawGame()
    {
    	canvas.clear();
    	canvas.background(getImage("Media/gameback.png"));   
    	
    	try {
		//	game.play();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	//updateBoard(); v
    	for(int j=0; j<=5; j++)
        	for(int i=0; i<=1;i++)
    		{
    			int k = gamegrid[i][j];
    			switch (k)
    			{
    			case 0: hole[i][j].setToFrame(0);
    					break;
    			case 1: hole[i][j].setToFrame(1);
    					break;
    			case 2: hole[i][j].setToFrame(2);
						break;
    			case 3: hole[i][j].setToFrame(3);
						break;
    			case 4: hole[i][j].setToFrame(4);
						break;
    			case 5: hole[i][j].setToFrame(5);
						break;
    			case 6: hole[i][j].setToFrame(6);
						break;
    			case 7: hole[i][j].setToFrame(7);
						break;
    			case 8: hole[i][j].setToFrame(8);
						break;
    			case 9: hole[i][j].setToFrame(9);
						break;
    			case 10: hole[i][j].setToFrame(10);
						break;
    			case 11: hole[i][j].setToFrame(11);
						break;
    			case 12: hole[i][j].setToFrame(12);
						break;
    			case 13: hole[i][j].setToFrame(13);
						break;
    			case 14: hole[i][j].setToFrame(14);
						break;
    			case 15: hole[i][j].setToFrame(15);
						break;
    			default: hole[i][j].setToFrame(15);
						break;
    			}    
    		}
    	
    	if (score1<=15)
    		scoreBoard1.setToFrame(score1);
    	else 
    		scoreBoard1.setToFrame(15);
    	
    	if (score2<=15)
    		scoreBoard2.setToFrame(score2);
    	else 
    		scoreBoard2.setToFrame(15);
    	
    	for(int i=0; i<=5; i++)
    		for(int j=0; j<=1;j++)    	
    			hole[j][i].draw();
    	
    	scoreBoard1.draw();
    	scoreBoard2.draw();
   	
    	canvas.font("Times New Roman", BOLD, 27, 0,0,0);
    	canvas.putText("[" + gamegrid[0][0] + "]", 255+40,140+115);    	
    	canvas.putText("[" + gamegrid[0][1] + "]", 375+40,140+115);  
    	canvas.putText("[" + gamegrid[0][2] + "]", 495+40,140+115);    	
    	canvas.putText("[" + gamegrid[0][3] + "]", 615+40,140+115); 
    	canvas.putText("[" + gamegrid[0][4] + "]", 735+40,140+115);    	
    	canvas.putText("[" + gamegrid[0][5] + "]", 855+40,140+115); 
    	
    	canvas.putText("[" + gamegrid[1][0] + "]", 255+40,340+115);    	
    	canvas.putText("[" + gamegrid[1][1] + "]", 375+40,340+115);  
    	canvas.putText("[" + gamegrid[1][2] + "]", 495+40,340+115);    	
    	canvas.putText("[" + gamegrid[1][3] + "]", 615+40,340+115); 
    	canvas.putText("[" + gamegrid[1][4] + "]", 735+40,340+115);    	
    	canvas.putText("[" + gamegrid[1][5] + "]", 855+40,340+115); 
    	
    	canvas.font("Times New Roman", BOLD, 50, 150,250,250);
    	canvas.putText("["+ score1 + "]", 90, 550);
    	canvas.putText("["+ score2 + "]", 1040, 550);
    	
    	System.out.println(move);
    	
    }
    public void updateBoard()
    {
    	
    }
    public void startOptionsScreen()
    {
    	canvas.background(getImage("Media/title.png"));
    	
    	player1 = makeButton("Player1",
                getImage("Media/Player1.png", 0, 255, 0),
                158, 30);
    	player2 = makeButton("Player2",
                getImage("Media/Player2.png", 0, 255, 0),
                158, 30);
    	player1.position(550,290+15);
    	player2.position(550,390+15);
    	canvas.font("Times New Roman", BOLD, 20);
    }
  
    public void drawOptionsScreen()
    {
    	canvas.clear();
    	canvas.putText(players[p1], 590, 290);
    	canvas.putText(players[p2], 590, 390);
    	player1.draw();
    	player2.draw();    	
    	button1.draw();
    }
    public void onClickPlayer1()
    {
    	p1++;
    	p1 = p1%2;
    	
    }
    public void onClickPlayer2()
    {
    	p2++;
    	p2 = p2%2;
    }
    public void startInstrucScreen()
    {
    	
    }
    public void drawInstrucScreen()
    {
    	canvas.clear();
    	canvas.background(getImage("Media/instructions.png"));
    	button1.draw();
    	button2.draw();
    }
    public void draw()
    {
        canvas.clear();        
    }
    public void onMousePressed()
    {
      if (mouse.sprite() == hole[0][0])
      	move = 00;
      else if (mouse.sprite() == hole[0][1])
    	move = 01;
      else if (mouse.sprite() == hole[0][2])
  		move = 02;
      else if (mouse.sprite() == hole[0][3])
  		move = 03;
      else if (mouse.sprite() == hole[0][4])
  		move = 04;
      else if (mouse.sprite() == hole[0][5])
  		move = 05;
      
      else  if (mouse.sprite() == hole[1][0])
    		move = 10;
      else if (mouse.sprite() == hole[1][1])
  		move = 11;
      else if (mouse.sprite() == hole[1][2])
		move = 12;
      else if (mouse.sprite() == hole[1][3])
		move = 13;
      else if (mouse.sprite() == hole[1][4])
		move = 14;
      else if (mouse.sprite() == hole[1][5])
		move = 15;
    } 
   
  
}